package com.schedule.teacher.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.schedule.common.CommonResult;
import com.schedule.teacher.entity.Teacher;
import com.schedule.teacher.entity.TeacherParam;
import com.schedule.teacher.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author cyljx
 */
@RestController
@RequestMapping("/api/teacher")
public class TeacherController {
    @Autowired
    private TeacherService teacherService;


    @PostMapping
    public CommonResult<String> add(@RequestBody Teacher teacher){
        boolean save = teacherService.save(teacher);
        if(save){
            return CommonResult.success("新增成功！");
        }
        return CommonResult.error("新增失败!");
    }


    @PutMapping
    public CommonResult<String> edit(@RequestBody Teacher teacher){
        boolean save = teacherService.updateById(teacher);
        if(save){
            return CommonResult.success("编辑成功！");
        }
        return CommonResult.error("编辑失败!");
    }


    @DeleteMapping("/{teacherId}")
    public CommonResult<String> delete(@PathVariable("teacherId") Long teacherId){
        boolean b = teacherService.removeById(teacherId);
        if(b){
            return CommonResult.success("删除成功！");
        }
        return CommonResult.error("删除失败!");
    }

    @GetMapping("/list")
    public CommonResult<IPage<Teacher>> getList(TeacherParam param){
        IPage<Teacher> list = teacherService.getList(param);
        return CommonResult.success("查询成功",list);
    }
}
